package com.bdqn.wang.servlet;

import com.bdqn.wang.UserPage.Page;
import com.bdqn.wang.pojo.Menber;
import com.bdqn.wang.service.MenberService;
import com.bdqn.wang.service.UserService;
import com.bdqn.wang.service.impl.MenberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(urlPatterns = {"/user"})
public class MenberServlet extends HttpServlet {
    MenberService userService=new MenberServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        List<Menber> infolist=null;

        String opr = req.getParameter("opr");
        if (opr.equals("userlist")){

            int currentPage = 0;
            if (req.getParameter("currentPage") == null) {
                currentPage = 1;
            } else {
                currentPage = Integer.parseInt(req.getParameter("currentPage"));
            }
            Page  page=userService.getAllByPage(currentPage);
            req.getSession().setAttribute("p",page);
            req.getRequestDispatcher("/WEB-INF/User/user.jsp").forward(req,resp);
//            infolist=userService.getAll();
//            req.getSession().setAttribute("user",infolist);
//            req.getRequestDispatcher("/WEB-INF/User/user.jsp").forward(req,resp);
        }else if (opr.equals("getOne")){
            int id=Integer.parseInt(req.getParameter("id"));
            Menber user=userService.getOne(id);
            req.getSession().setAttribute("user",user);
            req.getRequestDispatcher("/WEB-INF/User/updateUser.jsp").forward(req,resp);
        }
        else if(opr.equals("updateUser")){
            Menber user=new Menber();
            user.setId(Integer.parseInt(req.getParameter("id")));
            user.setLoginName(req.getParameter("loginName"));
            user.setUserName(req.getParameter("userName"));
            user.setSex(Integer.parseInt(req.getParameter("sex")));
            user.setType(req.getParameter("type"));
            int count=userService.update(user);
            if (count>0){
                int currentPage = 0;
                if (req.getParameter("currentPage") == null) {
                    currentPage = 1;
                } else {
                    currentPage = Integer.parseInt(req.getParameter("currentPage"));
                }
                Page  page=userService.getAllByPage(currentPage);
                req.getSession().setAttribute("p",page);
                req.getRequestDispatcher("/WEB-INF/User/user.jsp").forward(req,resp);
            }

        }else if (opr.equals("delteUser")){

            int id=Integer.parseInt(req.getParameter("id"));
            int count=userService.delet(id);
            if (count>0){
                int currentPage = 0;
                if (req.getParameter("currentPage") == null) {
                    currentPage = 1;
                } else {
                    currentPage = Integer.parseInt(req.getParameter("currentPage"));
                }
                Page  page=userService.getAllByPage(currentPage);
                req.getSession().setAttribute("p",page);
                req.getRequestDispatcher("/WEB-INF/User/user.jsp").forward(req,resp);
            }
        }else if(opr.equals("getadd")){
            req.getRequestDispatcher("/WEB-INF/User/addUser.jsp").forward(req,resp);
        }
        else if (opr.equals("addUser")){
            Menber user=new Menber();

            user.setLoginName(req.getParameter("loginName"));
            user.setUserName(req.getParameter("userName"));
            user.setSex(Integer.parseInt(req.getParameter("sex")));
                 user.setIdentityCode(req.getParameter("identityCode"));
                 user.setMobile(req.getParameter("mobile"));
                 user.setEmail(req.getParameter("Email"));
            user.setPassword(req.getParameter("password"));
            int count=userService.add(user);
            if (count>0){
                infolist=userService.getAll();
                req.getSession().setAttribute("user",infolist);
                req.getRequestDispatcher("/WEB-INF/User/user.jsp").forward(req,resp);
            }
        }else if (opr.equals("fenye")){
            int currentPage = 0;
            if (req.getParameter("currentPage") == null) {
                currentPage = 1;
            } else {
                currentPage = Integer.parseInt(req.getParameter("currentPage"));
            }
            Page  page=userService.getAllByPage(currentPage);
            req.getSession().setAttribute("p",page);
            req.getRequestDispatcher("/WEB-INF/User/user.jsp").forward(req,resp);
        }

    }
}
